package com.petAdoption.manager.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.petAdoption.manager.entity.Product;
import com.petAdoption.manager.entity.Productswipe;
import com.petAdoption.manager.service.IProductService;
import com.petAdoption.manager.service.IProductswipeService;
import com.petAdoption.manager.util.Result;
import com.petAdoption.manager.vo.ProductSwipeVO;
import com.petAdoption.manager.vo.ProductVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 王倩茹
 * @since 2023-08-28
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Resource
    private IProductService productService;

    @Resource
    private IProductswipeService productswipeService;

    @Resource
    private Result result;

    @GetMapping("/findAll/{pageNum}")
    public Result findAll(@PathVariable Integer pageNum) {
        PageHelper.startPage(pageNum, 10);
        List<ProductVO> list = productService.findAll();
//        return result.success(new PageInfo<>(list));
        return result.success(new PageInfo<>(list));
    }


    @GetMapping("/findByName/{pageNum}")
    public Result findByName(String productName, @PathVariable Integer pageNum) {
        PageHelper.startPage(pageNum, 10);
        List<ProductVO> list = productService.findByName(productName);
        return result.success(new PageInfo<>(list));
    }


    @PutMapping("/updateProduct")
    public Result updateProduct(@RequestBody Product product) {
        int rows = productService.updateProduct(product);
        if (rows > 0) {
            return result.success("修改成功", rows);
        } else {
            return result.fail("修改失败");
        }
    }


    @DeleteMapping("/delProduct")
    public Result delProduct(Integer productId) {
        int rows = productService.delProduct(productId);
        if (rows > 0) {
            return result.success("删除成功", rows);
        } else {
            return result.fail("删除失败");
        }
    }


    @PostMapping("/addProduct")
    public Result addProduct(@RequestBody Product product) {
        int rows = productService.addProduct(product);
        if (rows > 0) {
            return result.success("添加成功", rows);
        } else {
            return result.fail("添加失败");
        }

    }

    //添加单个商品的轮播图
    @PostMapping("/productswipe")
    public Result addProductSwipe(ProductSwipeVO productSwipeVO) {
        List<Productswipe> list = productSwipeVO.getProductswipes();
        list.forEach(Productswipe -> Productswipe.setProductswipeId(productSwipeVO.getProductId()));
        productswipeService.addProductSwipe(list);
        Product product = new Product();
        product.setProductSwipe(productSwipeVO.getProductId());
        productService.update(product, new UpdateWrapper<Product>().eq("product_id", productSwipeVO.getProductId()));
        return result.success();

    }


    @GetMapping("/productTypeName")
    public Result findProducttype(@PathVariable int productType) {
        List<ProductVO> list = productService.productTypeName(productType);
        if (list != null && !list.isEmpty()) {
            return result.success("查找成功", list);
        }
        return result.fail("查找失败");
    }


    @GetMapping("/productTypeName/{productType}")
    public Result findProductTypeById(@PathVariable int productType) {
        List<ProductVO> list = productService.productTypeById(productType);
        if (list != null && !list.isEmpty()) {
            return result.success("查找成功", list);
        }
        return result.fail("查找失败");
    }


}
